package code.cando;

/**
 * author : yeswater
 * date : 2023/12/5
 * <p>
 * 盛最多水的容器
 * 两条线之间能装的水最多!!!
 */

public class T011 {
    public static void main(String[] args) {
        int[] nums = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        // int[] nums = {1, 1};
        // int[] nums = {1,2, 1};
        System.out.println(maxArea(nums));
    }

    // 部分输入超时
    public static int maxArea1(int[] height) {
        int res = 0;
        int length = height.length;
        for (int i = 0; i < length; i++) {
            for (int j = length - 1; j > i; j--) {
                res = Math.max(res, (j - i) * (Math.min(height[i], height[j])));
            }
        }
        return res;
    }

    // 用好 while 循环 和三元运算符 √
    public static int maxArea(int[] height) {
        int i = 0;
        int j = height.length - 1;
        int res = 0;
        while (i < j) {
            res = height[i] < height[j] ? Math.max(res, (j - i) * height[i++]) : Math.max(res, (j - i) * height [j--]);
        }
        return res;
    }
}
